<template>
  <page-header-wrapper>
    <page-tab-list></page-tab-list>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 使用中 </a-radio-button>
          <a-radio-button value="0"> 已失效 </a-radio-button>
        </a-radio-group>
        <div>
          <a-button type="primary" @click="$router.push({ path: '/saas/marketing/vip/edit' })">+ 新增</a-button>
          <!-- <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="allDelete">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown> -->
        </div>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.pic_url" alt="" />
            </div>
            <div class="info">
              <span>{{ record.name }}</span>
              <span>规格：{{ record.attr | sku }}</span>
            </div>
          </div>
        </div>
        <div slot="status" slot-scope="text" class="status">
          <span :class="text == 1 ? 'open' : 'close'"></span>
          <span v-if="text == 0">已失效</span>
          <span v-if="text == 1">使用中</span>
        </div>
        <div slot="action" slot-scope="scope">
          <a-button type="link" size="small" @click="$router.push({ path: '/saas/marketing/vip/edit?id=' + scope.id })"
            >编辑</a-button
          >
          <a-divider type="vertical" style="margin:0 1px;"/>
          <a-dropdown>
            <a-button type="link" size="small"> 更多 <a-icon type="down" /> </a-button>
            <a-menu slot="overlay">
              <a-menu-item v-if="scope.status == 1">
                <a-button type="link" size="small" @click="changeStatus(scope,0)">关闭</a-button>
              </a-menu-item>
              <a-menu-item v-if="scope.status == 0">
                <a-button type="link" size="small" @click="changeStatus(scope,1)">开启</a-button>
              </a-menu-item>
              <a-menu-item>
                <a-button type="link" size="small" @click="delVipCard(scope)">删除</a-button>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { vipCardList, changeVipStatus,delVipCard } from '@/api/saas/marketing'
import { cyyUploadImg } from '@/components/CyyUi'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '会员卡名称',
    dataIndex: 'name',
  },
  {
    title: '排序',
    dataIndex: 'sort',
    sorter: (a, b) => a.sort - b.sort,
  },
  {
    title: '购卡量',
    dataIndex: 'num',
    sorter: (a, b) => a.num - b.num,
  },
  {
    title: '累计付费金额',
    dataIndex: 'total_amount',
    sorter: (a, b) => a.total_amount - b.total_amount,
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    width: 160,
    scopedSlots: {
      customRender: 'action',
    },
  },
]
export default {
  components: {
    cyyUploadImg,
  },
  data() {
    return {
      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
        status: '-1',
      },
      selectedRowKeys: [],
      columns,
      list: [],
      btnLoading: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await vipCardList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.data
        this.totalCount = res.data.totalCount
      }
      hide()
    },
    // 点击开启或者关闭会员卡
    changeStatus(scope, status) {
      const that = this
      let text = status == 0? '关闭':'开启'
      this.$confirm({
        content: '确定要'+text+'该会员卡吗?',
        onOk: () => {
          let params = {
            id:scope.id,
            status
          }
          that.change(params)
        },
      })
    },
    // 切换状态
    async change(params) {
      const res = await changeVipStatus(params)
      if(res.code == 0) {
        this.getList()
      }
    },
    // 点击删除会员卡
    delVipCard(scope) {
      const that = this
      this.$confirm({
        content: '确定删除该会员卡吗?',
        onOk(){
          delVipCard({id:scope.id}).then(res => {
            if(res.code == 0) {
              that.getList()
            }
          })
        },
      })
    }
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 8px;
  }
  .open {
    background-color: #65c817;
  }
  .close {
    background-color: #c6c6c6;
  }
}
</style>
